package com.zlzxm.sorting.ui.adapter

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zlzxm.sorting.R
import com.zlzxm.sorting.ui.adapter.entity.EntivityPrintDevice

class AdapterPrintDevice:BaseQuickAdapter<EntivityPrintDevice,BaseViewHolder>(R.layout.item_print_device) {

    var selectPosition = -1

    override fun convert(holder: BaseViewHolder, item: EntivityPrintDevice) {
        holder.itemView.isSelected = item.isSelect
        holder.setText(R.id.tvTitle,item.title)
    }

    override fun setNewInstance(list: MutableList<EntivityPrintDevice>?) {
        super.setNewInstance(list)
        for ((index,datum) in data.withIndex()) {

            if(datum.isSelect){
                selectPosition = index
                return
            }

        }
    }

    override fun setOnItemClick(v: View, position: Int) {
        if(selectPosition == -1){

            data[position].isSelect = true
            selectPosition = position

            notifyItemChanged(position)

        }else   if(position == selectPosition){

            data[position].isSelect = false
            selectPosition = -1
            notifyItemChanged(position)
        } else{

            data[selectPosition].isSelect = false
            data[position].isSelect = true
            notifyItemChanged(selectPosition)
            notifyItemChanged(position)
            selectPosition = position
        }
        super.setOnItemClick(v, position)
    }


    fun selectPosition(position:Int){

        if(position == -1){

            if(selectPosition != -1){

                data[selectPosition].isSelect = false
                notifyItemChanged(selectPosition)
                selectPosition = -1

            }

        }else{

            if(selectPosition != -1){

                data[selectPosition].isSelect = false
                notifyItemChanged(selectPosition)

            }

            data[position].isSelect = true
            notifyItemChanged(position)
            selectPosition = position

        }

    }


}